package ThreadDemo3;

/**
 * Created with IntelliJ IDEA.
 * Description:锁操作
 * User: 阿宾
 * Date: 2025-03-24
 * Time: 20:01
 */
public class ThreadDemo1 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        //创建一个对象
        Object locker1 = new Object();
        Object locker2 = new Object();


        //创建两个线程，每个线程对count加5W次
        Thread t1 = new Thread(
                ()->{
                    for (int i = 0; i < 50000; i++) {
                        synchronized (locker1){
                            count++;
                        }
                    }
                }
        );
        Thread t2 = new Thread(
                ()->{
                    for (int i = 0; i < 50000; i++) {

                            count++;

                    }
                }
        );
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        //创建了两个对象，结果还是错误的，存在线程安全问题
        System.out.println("count = "+count);
    }
}